package com.zhn.usermanager.service.impl;

import com.zhn.usermanager.mapper.UserMapper;
import com.zhn.usermanager.pojo.User;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传
 */
@Service
public class FileUpLoadServiceImpl {

    @Resource
    private UserMapper userMapper;

    /**
     * 上传文件处理
     * @param file           接收上传的文件对象
     * @param request        请求对象
     * @param abPath         存放的文件夹名称
     * @return               返回上传成功与否信息
     * @throws IOException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @Transactional
    public Map<String, Object> fileUpLoad(MultipartFile file, HttpServletRequest request, String abPath) throws IOException, SQLException, ClassNotFoundException {
        Map<String, Object> map = new HashMap<>();
        String uploadPath = request.getServletContext().getRealPath("") + File.separator + abPath + File.separator;   // 得到相当项目工程的硬盘物理地址
        String fileName = file.getOriginalFilename();                   // 获取上传的文件名
        File newFile = new File(uploadPath + fileName);         // 根据当前工程的物理路径+文件名生成一个file对象
        System.out.println("newFile=" + newFile);
        FileCopyUtils.copy(file.getBytes(), newFile);                   // 通过上传通道中的字节码完成file文件的写入操作
//        UserJDBC userJDBC = new UserJDBC();
//        User user = (User)request.getSession().getAttribute("user");
//        String userSQLPath = File.separator + abPath + File.separator + fileName;  // 组装一个浏览器识别的文件地址
//        System.out.println("header=" + userSQLPath);
//        userJDBC.userUpdateForHeader(userSQLPath, "" + user.getId());

        String path = newFile.toString();                                  // 获取文件的物理路径
        InputStream is = new FileInputStream(path);;                       // 生成输入流
        HSSFWorkbook workbook = new HSSFWorkbook(is);                      // HSSFWorkbook类型对应2003版本
        User user = null;
        Sheet sheet = workbook.getSheetAt(0);                       //模板数据默认读取第一个工作页sheet，index默认从0开始，如果存在多个sheet，那么需要循环Sheet判断
//        for (Row row : sheet) {                                            //循环读取第一个工作页中的每一行记录
//            for (Cell cell : row) {                                        // 循环读取一行中的每一列数据
//                cell.setCellType(CellType.STRING);                         // 根据不同类型转化成字符串
//                System.out.print(cell.getStringCellValue() + "    ");            // 获取当前列中的数据
//            }
//        }

//        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);  // 把多条插入操作合并为一条sql，一次性传递给mysql执行
//        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        for(int i = 1; i <= sheet.getLastRowNum(); i++) {
            if(i == 3) Integer.parseInt("a");
            Row row = sheet.getRow(i);
            user = new User();
            Cell cell = row.getCell(0);
            cell.setCellType(CellType.STRING);
            user.setPhone(cell.getStringCellValue());
            cell = row.getCell(1);
            cell.setCellType(CellType.STRING);
            user.setEmail(cell.getStringCellValue());
            cell = row.getCell(2);
            cell.setCellType(CellType.STRING);
            user.setPassword(cell.getStringCellValue());
            cell = row.getCell(3);
            cell.setCellType(CellType.STRING);
            user.setUsername(cell.getStringCellValue());
            cell = row.getCell(4);
            cell.setCellType(CellType.STRING);
            user.setAction(Integer.parseInt(cell.getStringCellValue()));
            userMapper.insert(user);
        }
        map.put("code", 200);
        map.put("msg", "上传用户数据成功");
//        map.put("data", userSQLPath);
        return map;
    }
}
